package org.dromara.system.controller.xcx;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import net.coobird.thumbnailator.Thumbnails;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.MimeTypeUtils;
import org.dromara.system.domain.vo.AvatarVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 上传图片
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xcx/oss")
public class XcxOssController {
    private final ISysOssService ossService;
    /**
     * 添加图片
     */
    @Log(title = "上传图片", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<List<AvatarVo>> avatar(@RequestPart("avatarfile") List<MultipartFile> avatarfiles) {
        //判断集合是否为空
        if (CollectionUtils.isEmpty(avatarfiles)) {
            System.out.println("请至少选择一张图片进行上传");
            return R.fail("请至少选择一张图片进行上传");
        }
        //1
        //图片对象集合
        List<AvatarVo> avatarVoList = new ArrayList<>();
        //循环集合
        for (MultipartFile avatarfile : avatarfiles) {
            //判断图片是否为空
            if (!avatarfile.isEmpty()) {
                String extension = FileUtil.extName(avatarfile.getOriginalFilename());
                //判断图片格式是否正确
                if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                    return R.fail("文件格式不正确，请上传 " + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + " 格式图片");
                }
                //压缩图片
                try {
                    // 将字节数组转换为 BufferedImage
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(avatarfile.getBytes());
                    BufferedImage bufferedImage = Thumbnails.of(inputStream).scale(1).asBufferedImage();

                    // 对图片进行压缩
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    Thumbnails.of(bufferedImage)
                        .size(1200, 900) // 设置压缩后的尺寸
                        .outputQuality(0.8)// 设置压缩质量，范围是 0 到 1
                        .outputFormat(extension)//设置文件后缀名
                        .toOutputStream(outputStream); // 将压缩后的图片写入输出流
                    // 获取压缩后的字节数组
                    byte[] compressedImageData = outputStream.toByteArray();
                    System.out.printf(avatarfile.toString());

                    // 创建压缩后的MultipartFile对象
                    avatarfile = new MockMultipartFile(avatarfile.getName(),avatarfile.getOriginalFilename(),avatarfile.getContentType(),compressedImageData);
                    //上传图片
                    SysOssVo oss = ossService.upload(avatarfile);
                    //获取图片的url
                    String avatarUrl = oss.getUrl();
                    AvatarVo avatarVo = new AvatarVo();
                    avatarVo.setImgUrl(avatarUrl);
                    avatarVoList.add(avatarVo);
                    //通过反射修改图片
//                    Field field = avatarfile.getClass().getField("content");
//                    field.setAccessible(true);
//                    field.set(avatarfile,compressedImageData);
                } catch (IOException e) {
                    e.printStackTrace();
                }




            } else {
                return R.fail("上传的图片为空，请检查后重新上传");
            }
        }
        return R.ok(avatarVoList);
    }




}
